﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1.SensorTable
{
    public partial class FormMobile : Form
    {
        private bool change = false;

        public FormMobile()
        {
            InitializeComponent();
        }

        private void tb_mobileNumBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            DataTable daorig = new DataTable();
            DataTable dadest = new DataTable();

            int[] check = new int[2] { 0, 0};
            try
            {
                SqlConnection sqlCon = new SqlConnection(UserLog.connectionstring);
                SqlDataAdapter sda = new SqlDataAdapter();
                SqlDataAdapter sda2 = new SqlDataAdapter();


                string strSql = "select * from tb_mobileNum";
                SqlCommand cmd = new SqlCommand(strSql, sqlCon);
                sda.SelectCommand = cmd;
                sda.Fill(daorig);
                tb_mobileNumDataGridView.EndEdit();
                Monitor_DB.instance.db.SubmitChanges();
                change = false;

                sda.Fill(dadest);

                UserLog.addrecordfordatagridview(daorig, dadest, UserLog.phonenum, "配置手机号", "成功", check);

                MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception Ex)
            {
                UserLog.addrecordfordatagridview(daorig, dadest, UserLog.phonenum, "配置手机号", "失败", check);
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Monitor_DB.instance.Refresh_DB();
            }
        }

        private void FormMobile_Load(object sender, EventArgs e)
        {
            try
            {
                Monitor_DB.instance.db.Refresh(RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_mobileNum);
                this.tb_mobileNumBindingSource.DataSource = Monitor_DB.instance.db.tb_mobileNum;

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tb_mobileNumDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.S)
            {
                tb_mobileNumBindingNavigatorSaveItem_Click(sender, e);
            }
        }

        private void FormMobile_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (change)
            {
                DialogResult r = MessageBox.Show("保存修改？", "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (r == DialogResult.Yes)
                {
                    tb_mobileNumBindingNavigatorSaveItem_Click(sender, e);
                }
                else if (r == DialogResult.Cancel)
                {
                    e.Cancel = true;

                }
            }
        }

        private void tb_mobileNumDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            change = true;
        }

        private void mobile_numTextBox_Click(object sender, EventArgs e)
        {
            change = true;
        }
    }
}
